using NewTry;
using System.Threading.Tasks.Dataflow;

namespace TPL
{
    //class Program
    //{
    //    static async Task Main(string[] args)
    //    {
    //        string filePathA = "inputc.bin";
    //        int bufferSize = 15;
    //        byte[] pattern = { 0x01, 0x02, 0x03, 0x04 };

    //        _FileReadBlock fileReadBlock1 = new _FileReadBlock(filePathA, bufferSize);
    //        SegmentBlock segmentBlock1 = new SegmentBlock(pattern);
    //        ConsoleWriteBlock consoleWriteBlock = new ConsoleWriteBlock();

    //        fileReadBlock1.DataArrived += (data) =>
    //        {
    //            segmentBlock1.Enqueue(data);
    //        };
    //        segmentBlock1.DataArrived += (data) =>
    //        {
    //            consoleWriteBlock.Enqueue(data);
    //        };
    //        Task readTask = Task.Run(() => fileReadBlock1.ReadBytes());
    //        // 等待控制台输出块和分段块完成
    //        // 确保所有任务都已完成
    //        await Task.WhenAll(
    //            segmentBlock1._Process.Completion,
    //            consoleWriteBlock._InputBlock.Completion
    //        );
    //        // 确保所有任务都已完成
    //        readTask.Wait(); // 等待文件读取任务完成
    //        Console.WriteLine("所有Block处理完成，主程序结束。");
    //    }
    //}
    class Program
{
    static async Task Main(string[] args)
    {
        string filePathA = "inputa.bin";
        string filePathB = "inputc.bin";
        int bufferSize = 15;
        byte[] pattern = { 0x01, 0x02, 0x03, 0x04 };

        _FileReadBlock fileReadBlock1 = new _FileReadBlock(filePathA, bufferSize);
        _FileReadBlock fileReadBlock2 = new _FileReadBlock(filePathB, bufferSize);
        SegmentBlock segmentBlock1 = new SegmentBlock(pattern);
        SegmentBlock segmentBlock2 = new SegmentBlock(pattern);

        CompareBlock compareBlock = new CompareBlock();
        ConsoleResultBlock consoleResultBlock = new ConsoleResultBlock();

        fileReadBlock1.DataArrived += (data) =>
        {
            segmentBlock1.Enqueue(data);
        };

        fileReadBlock2.DataArrived += (data) =>
        {
            segmentBlock2.Enqueue(data);
        };
        segmentBlock1.DataArrived += (data) =>
        {
            compareBlock.Enqueue_1(data);
        };
        segmentBlock2.DataArrived += (data) =>
        {
            compareBlock.Enqueue_2(data);
        };

        compareBlock.DataArrived += (data) =>
        {
            consoleResultBlock.Enqueue(data);
            if (!data.Item1) // 如果发现不同
            {
            //    //segmentBlock1._Process.Complete(); // 停止分帧模块
            //    //segmentBlock2._Process.Complete();
                compareBlock.compareBlock.Complete(); // 停止对比模块
                consoleResultBlock._InputBlock.Complete();
            }
        };
        Task readTask1 = Task.Run(() => fileReadBlock1.ReadBytes());
        Task readTask2 = Task.Run(() => fileReadBlock2.ReadBytes());
        // 确保所有任务都已完成
        readTask1.Wait(); // 等待文件读取任务完成
        readTask2.Wait();
        // 等待控制台输出块和分段块完成
        // 确保所有任务都已完成
        await Task.WhenAll(
            //segmentBlock1._Process.Completion, segmentBlock2._Process.Completion, compareBlock.compareBlock.Completion,
            consoleResultBlock._InputBlock.Completion
        );
        // 如果所有文件都相同，则输出结果
        //if (!compareBlock.IsDifferent)
        //{
        //    Console.WriteLine("所有文件相同");
        //}
        Console.WriteLine("所有Block处理完成，主程序结束。");
    }
}
}